<?php
defined('IN_MAIN') or exit('Invalid Access!');

function is_group_ref_downline_of($uid, $upline_id){ 
	global $vars; 
	$db=$vars["db"]; 
	$yes=false; 
	if($uid && $upline_id){  
		$id=$uid;  
		do{   
			$ref=@mysql_result(mysql_query("select ref from $db->users where id='$id'"), 0);   
			if($ref!=$upline_id){    
				$id=$ref;   
			}else{    
				$yes=true;   
			}  
		}while(!$yes && $id); 
	} return $yes;
}

function get_sponsor_downline($uid, $code){
	global $vars; 
	$db=$vars["db"]; 
	
	if($code>0){
		$uid=@mysql_result(mysql_query("select id from $db->users where code='$code'"), 0);		
	}	
	
	$total=@mysql_num_rows($r=mysql_query("select d.code, d.username from $db->users d inner join $db->users s on s.id='$uid' and d.ref_left between s.ref_left and s.ref_right"));
	$options = "";
	
	for($i=0;$i<$total;$i++){
		$member=mysql_fetch_assoc($r);
		$options.="<option value='".$member['code']."' ".($code==$member['code']?'selected':'').">".$member['code']."</option>";		
	}
	return $options;
}

function get_placement_downline($uid, $code){
	global $vars; 
	$db=$vars["db"]; 
	
	if($code>0){
		$uid=@mysql_result(mysql_query("select id from $db->users where code='$code'"), 0);		
	}
	
	$total=@mysql_num_rows($r=mysql_query("select d.code, d.username from $db->users d inner join $db->users s on s.id='$uid' and d.matrix_left between s.matrix_left and s.matrix_right"));
	$options = "";
	
	for($i=0;$i<$total;$i++){
		$member=mysql_fetch_assoc($r);
		$options.="<option value='".$member['code']."' ".($code==$member['code']?'selected':'').">".$member['code']."</option>";		
	}
	return $options;
}

function get_left_code($code){
	global $vars; 
	$db=$vars["db"]; 
	$left_code = $code;
	
	while($code<>""){
		$member=@mysql_result(mysql_query("select d.code from $db->users d inner join $db->users s on s.code='$code' and d.matrix_upline=s.id and d.matrix_position='1'"), 0);
		if($member>0){
			$left_code = $member;
			$code = $member;
		}else{
			$code = "";
			return $left_code;
		}
	}
}

function get_right_code($code){
	global $vars; 
	$db=$vars["db"]; 
	$left_code = $code;
	
	while($code<>""){
		$member=@mysql_result(mysql_query("select d.code from $db->users d inner join $db->users s on s.code='$code' and d.matrix_upline=s.id and d.matrix_position='2'"), 0);
		if($member>0){
			$left_code = $member;
			$code = $member;
		}else{
			$code = "";
			return $left_code;
		}
	}
}
?>